/*
Copyright 2010 Peter Krajnc, SCREENWORKER

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.screenworker.compactmvc.patterns
{
	import com.screenworker.compactmvc.core.CompactMVC;
	import com.screenworker.compactmvc.interfaces.IModel;
	import com.screenworker.compactmvc.vo.NoteVO;
	
	import flash.display.Stage;
	
	/**
	 * <code>IModel</code> implementation.
	 * Pattern Class to be extended by subclasses
	 * 
	 * <code>Model</code> classes are used for example to manage data of view classes.
	 * It is also used to encapsulate interaction with remote services to distribute data over the Application.
	 * Usage to distribute Data: 
	 * controller.sendNotification([notoficationType], [data]);
	 * 
	 * 
	 * <code>Model</code> classes are independent of View (UI) and <code>Controller</code>
	 * 
	 * @author Peter Krajnc
	 */
	public class Model implements IModel
	{
		private var _name:String;
		private var _controller:Controller;
	
		/**
		 * Constructor 
		 * @param name
		 * 
		 */		
		public function Model(name:String, controller:Controller)
		{
			_name = name;
			_controller = controller;
		}
		
		/**
		 * Called by the <code>Controller</code> when the <code>Model</code> is registered. 
		 * Your subclass should override this method if needed.
		 */
		public function registerHandler():void {}
		
		/**
		 * Called by the Controller when the <code>Model</code> is removed.
		 * Your subclass should override this method if needed
		 */		
		public function removeHandler():void {}
		
		/**
		 * returns the modelname 
		 * @return 
		 * 
		 */		
		public function getName():String
		{
			return _name;
		}
		
		/**
		 * retrieve Model 
		 * @param modelName
		 * @return 
		 * 
		 */		
		protected function retrieveModel(modelName:String):IModel
		{
			return _controller.retrieveModel(modelName);
		}
		
		/**
		 * register Model 
		 * @param modelName
		 * @param modelClassRef
		 * 
		 */		
		protected function registerModel(modelName:String, modelClassRef:Class):void
		{	
			_controller.registerModel(modelName, modelClassRef);
		}
		
		/**
		 * remove Model 
		 * @param modelName
		 * 
		 */		
		protected function removeModel(modelName:String):void
		{	
			_controller.removeModel(modelName);
		}
		
				/**
		 * remove View(UI) and Mediator 
		 * @param viewName
		 * 
		 */		
		protected function removeView(viewName:String ):void
		{
			_controller.removeView(viewName);
		}
		
		
		protected function retrieveView(viewname:String):Object
		{
			return _controller.retrieveView(viewname);
		}
		
		protected function isViewRegistered(viewname:String):Boolean
		{
			return _controller.isViewRegistered(viewname);
		}
		
		/**
		 * returns all registered views 
		 * @return 
		 * 
		 */		
		protected function retrieveAllRegisteredViews():Array
		{
			return _controller.retrieveAllRegisteredViews();
		}
		
		
		/**
		 * retrieves the mainApplication object
		 * @return 
		 * 
		 */		
		protected function retrieveMainApplication():*
		{
			return _controller.mainApplication;
		}
		
		
		/**
		 * 
		 * @param notificationType
		 * @param data
		 * @param delay (in milliseconds)
		 * 
		 */		
		protected function sendNotification(notificationType:String, data:Object=null, delay:Number=0):void
		{
			_controller.sendNotification( new NoteVO(notificationType, data), delay );
		}
		
		/**
		 * 
		 * @param controllerName
		 * @param notificationType
		 * @param data
		 * @param delay (in milliseconds)
		 * 
		 */		
		protected function sendNotificationTo(controllerName:String, notificationType:String, data:Object=null, delay:Number=0):void
		{
			CompactMVC.getInstance().sendNotificationTo(controllerName, notificationType, data, delay);
		}
		
		/**
		 * 
		 * @param notificationType
		 * @param data
		 * @param delay (in milliseconds)
		 * 
		 */				
		protected function sendJunctionNotification(notificationType:String, data:Object=null, delay:Number=0):void
		{	
			CompactMVC.getInstance().sendJunctionNotification( notificationType, data , delay);
		}
		
	}
}
